<template>
  <div class="app-container">
    <!-- <Swiper/> -->
    <a-tabs v-model:activeKey="params.orderby" centered :tabBarStyle="{background:'#fff'}" @change="tabChange" size="large">
      <a-tab-pane :key="item.key" :tab="item.tab" v-for="item in tabList"/>
    </a-tabs>
    <div class="card-list-content">
      <div class="card-list-spin" v-if="loading"><a-spin tip="数据加载中..." :spinning="loading"/></div>
      <div v-else>
        <div class="card-list" v-if="data?.length>0">
          <a-card :bordered="false" v-for="item in data" size="small" @click="goDetail(item.id)"> 
            <a-flex :gap="8">
              <a-avatar :src="staticURL +item.cover" shape="square" :size="90" />
              <div class="flex-1">
                <div class="card-list-title">{{ item.title }}</div>
                <div class="card-list-desc">{{ item.description }}</div>
              </div>
            </a-flex>
          </a-card>
        </div>
        <template v-else>
            <a-empty :image="simpleImage" />
        </template>
      </div>
      <div class="pagination">
        <a-pagination :show-total="(total:number) => `共 ${total} 条记录`"
            v-model:current="params.page"
            :total="total"
            show-size-changer 
            show-quick-jumper
            @change="pageChange"
          />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getCoffeeList } from '@/server/modules/home'
import { onMounted, reactive, ref } from 'vue'
import { Empty } from 'ant-design-vue';
// import Swiper from './components/Swiper.vue';
import { useRouter } from 'vue-router';
import { staticURL } from '@/config';

const simpleImage = Empty.PRESENTED_IMAGE_SIMPLE;
const router = useRouter()
const tabList = [
  {key:'id',tab:'最新'},
  {key:'recommend',tab:'推荐'},
  {key:'views',tab:'热门'},
  {key:'likes',tab:'点赞'}
]

const loading= ref<boolean>(false)

const data = ref<Coffee[]>([])
const params = reactive({
  orderby:'id',
  status:1 as number,
  page:1,
  page_size:10
})
const total = ref<number>(0)
const pageChange = (page:number,pageSize:number)=>{
  params.page = page
  params.page_size = pageSize
  setTimeout(()=>getDataList(),100)
}
const getDataList = async () => {
  if (loading.value) return
  try {
    loading.value = true
     const res = await getCoffeeList(params)
    if(res.code === 200) {
      data.value = res.data.data
      total.value = res.data.total
      params.page = res.data.page
      params.page_size = res.data.page_size
    }
  }finally{
    setTimeout(()=>{
      loading.value = false
    },500)
  }
}

onMounted(() => {
  getDataList()
 
})

const tabChange = () => { 
  getDataList()
}

const goDetail = (id:number) => { 
  router.push({
    name: 'CoffeeDetail',
    params: {
      id: id
    }
  })
}
</script>

<style scoped>
.card-list{
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  gap: var(--space-md);
  padding: var(--space-md);
}
:deep(.ant-tabs-nav){
  margin-bottom: 0;
}
.flex-1{
  flex:1
}
.card-list-desc,
.card-list-title {
  color: rgba(0, 0, 0, 0.88);
  font-weight: bold;
  font-size: 16px;
  line-height: 1.5;
  display: -webkit-box;
  overflow: hidden;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  box-orient: vertical;
}
.card-list-desc{
  font-size: 14px;
  font-weight: normal;
  color: var(--text-secondary);
}
.card-list-spin {
  text-align: center;
  padding-top: 32px;
}
.pagination {
  padding: 20px 0;
  text-align: center;
}
</style>